<?php

namespace app\admin\controller;

use app\common\model\Admin;
use app\common\model\Users;
use think\Controller;
use think\facade\Request;
use think\facade\View;
use think\Db;

use app\common\model\Project as ProjectModel;

class Project extends Base
{
    //
    public function index(){
        $where   = [];
        $keyword = Request::param('keyword');
        if (!empty($keyword)) {
            $where[] = ['name', 'like', '%' . $keyword . '%'];
        }
        $dateran = Request::param('dateran');
        if (!empty($dateran)) {
            $getDateran = get_dateran($dateran);
            $where[]    = ['create_time', 'between', $getDateran];
        }
        $list = ProjectModel::getList($where, $this->pageSize, ['id' => 'desc']);
        $all_articleNum = model('project')->select();
        $view = [
            'keyword'  => $keyword,
            'dateran'  => $dateran,
            'pageSize' => page_size($this->pageSize, $list->total()),
            'page'     => $list->render(),
            'list'     => $list,
            'empty'    => empty_list(9),
            'all_order_num' => $all_articleNum
        ];
        View::assign($view);
        return view();
    }
    public function add(){
        if(request()->isAjax()){
            $result = model('project')->save(input('post.'));
            if($result)
            {
                $this->success('成功',"admin/project/index");
            }else{
                $this->error('失败!',$_SERVER['HTTP_REFERER']);
            }
        }
        return view();
    }
    public function edit($id=0){
        if(request()->isAjax()){
            $data = input('post.');
            $result = model('project')->where('id',$id)->update($data);
            if($result)
            {
                $this->success('修改成功',"admin/project/index");
            }else{
                $this->error('修改失败!',$_SERVER['HTTP_REFERER']);
            }
        }

        $Info = model('project')->find($id);
        $viewData = [
            'Info'=>$Info
        ];
        $this->assign($viewData);
        return view();
    }
    public function del()
    {
        $id = Request::param('id');
        ProjectModel::destroy($id);
        return json(['error' => 0, 'msg' => '删除成功']);
    }

    //批量删除
    public function selectDel()
    {
        $id = Request::param('id');
        ProjectModel::destroy($id);
        return json(['error' => 0, 'msg' => '删除成功!']);
    }

    //结束项目
    public function project_end(){
        $id = Request::param('id');
        $project = ProjectModel::get($id);
        $project->status = 2;
        $res = $project->save();
        if ($res) {
            //给参加项目用户充值积分
            $all_vol = model('apply')->where('project_id',$id)->select();
//            var_dump($all_vol);die;
            foreach ($all_vol as $vol){
                //先结束申请
                Db::name('apply')->where('id', $vol['id'])->update(['status' => 7]);
//                var_dump($vol['id']);
                //然后查询用户信息充值
                var_dump($project->service_time);
                Db::name('users')
                    ->where('id', $vol['user_id'])->inc('integral',$project->integral)->inc('length_time',$project->service_time)->update();
//                $user = Users::get($vol['user_id']);
//                $user->lengthOfTime = ['inc',$project->service_time];
//                $user->integral = ['inc',$project->integral];
//                $user->save();
            }
            die;
            $this->success('操作成功',$_SERVER['HTTP_REFERER']);
        }else{
            $this->error('操作失败');
        }
    }

    //查询出已经报名的用户
    public function enlist($id){
        $users = model('apply')->where('project_id',$id)->field('user_id')->select()->toArray();
        $users_id_list = [];
        if($users){
            foreach ($users as $user_val){
                array_push($users_id_list,$user_val['user_id']);
            }
        }
        $vol_users = model('users')->where('id','in',$users_id_list)->field('id,vol_true_name')->select();
        $this->assign(compact('vol_users'));
        return view();
    }
}
